// pages/new/detail/detail.js
var app = new getApp()
var api = require('../../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    path:api.path,
    content:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user = api.cache("user")
    let id = options.id
    api.post('article',{id:id,member_id:user.member_id}).then(res => {
      if (res.code == 200) {
        this.setData({
          content: res.data
        })
      }
    })
    this.getCountHeight()
  },
  getCountHeight(){
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  operation(e){
    let user = api.cache("user")
    let res = api.checkLogin()
    if (!res) {
      return;
    }
    let index = e.currentTarget.dataset.index
    let url = ''
    if(index==1){
      if(this.data.content.is_fav==0){
        url = 'click_fav'
      }else{
        url = 'del_fav_single'
      }
    }else if(index==2){
      if(this.data.content.is_love==0){
        url = 'click_zan'
      }else{
        url = 'del_love_single'
      }
    }else if(index==3){
      url = 'click_share'
    }
    let params={
      id:this.data.content.article.id,
      data_type:2,
      member_id:user.member_id,
    }
    api.post(url,params).then(res=>{
      if(res.code==200){
        let content = this.data.content
        if(index==1){
          if(this.data.content.is_fav==0){
            content.fav_total +=1
            content.is_fav = 1
            api.tip("成功收藏")
          }else{
            content.fav_total -=1
            content.is_fav = 0
            api.tip("取消收藏")
          }
        }else if(index==2){
          if(this.data.content.is_love==0){
            content.love_total +=1
            content.is_love = 1
            api.tip("成功点赞")
          }else{
            content.love_total -=1
            content.is_love = 0
            api.tip("取消点赞")
          }
        }else if(index==3){
          content.share_total +=1
        }
        this.setData({
          content
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})